package com.sihai.ehr.service.impl;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.ExamAwardItem;
import com.sihai.ehr.model.biz.TrainCategory;
import com.sihai.ehr.service.ExamAwardItemService;

/**
 * 考核奖惩服务实现类
 * 
 * @author henry
 * 
 */
@Service("examAwardItemService")
public class ExamAwardItemServiceImpl implements ExamAwardItemService {
	@Resource
	DataAccessor dataAccessor;

	@Override
	public ExamAwardItem getExamAwardItem(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.get(ExamAwardItem.class, id);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取考核奖惩明细数据库发生异常】【异常说明：+"
					+ e.getMessage()
					+ "】【发生地点：ExamAwardItemServiceImpl.getExamAwardItem;参数id="
					+ id + " 】");
		}
	}

	@Override
	public LimitedList<ExamAwardItem> searchExamAwardItem(
			final Long organizationId, final int awardType, Limit limit,
			Sorter sorter) {
		if (organizationId == null || organizationId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(ExamAwardItem.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from ExamAwardItem e where cancel=0");
							dynamicQuery.appendIfNotNull(
									" and e.organization.id =:organizationId",
									"organizationId", organizationId);
							dynamicQuery.appendIfNotNull(
									" and e.awardType =:awardType",
									"awardType", awardType);
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核奖惩时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamAwardItemServiceImpl.searchExamAwardItem;参数organizationId="
							+ organizationId + "】");
		}
	}
	
	@Override
	public LimitedList<ExamAwardItem> searchExamAwardItemList(
			final Long organizationId, final int awardType, Limit limit,
			Sorter sorter) {
		if (organizationId == null || organizationId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByHql(ExamAwardItem.class, "select new ExamAwardItem(id,title,awardType) from ExamAwardItem e where cancel=0 and e.organization.id =" + organizationId
					+ " and e.awardType =" + awardType, null, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核奖惩时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamAwardItemServiceImpl.searchExamAwardItem;参数organizationId="
							+ organizationId + "】");
		}
	}

	@Override
	public void saveExamAwardItem(ExamAwardItem examAwardItem) {
		if (examAwardItem == null || examAwardItem.getOrganization() == null) {
			throw new RuntimeException("【category对象参数异常】");
		}
		try {
			dataAccessor.saveOrUpdate(examAwardItem);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保培考核奖惩对象到数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：ExamAwardItemServiceImpl.saveExamAwardItem】");
		}
	}

	@Override
	public void removeExamAwardItem(final String examAwardItems) {
		if (examAwardItems == null || examAwardItems.isEmpty()) {
			throw new RuntimeException("【参数异常：examAwardItems为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat.format(
							"delete from ExamAwardItem e where e.id in ({0})",
							examAwardItems));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：移出考核奖惩信息数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：ExamAwardItemServiceImpl.removeExamAwardItem】");
		}
	}

}
